package com.hhs.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.ZrReportService;
import com.hhs.storage.service.impl.BaseDepartmentServiceImpl;
import com.hhs.storage.util.SysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuanjun
 * @since 2024-01-12
 */
@RestController
@RequestMapping("/zrReport")
@ApiSupport(order = 4)      //排序
@Api(tags = "总结报告接口")
public class ZrReportController {
    @Resource
    private ZrReportService zrReportService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private BaseDepartmentServiceImpl baseDepartmentService;




    /**
     * 根据id查找所属部门
     * @param baseDepartment
     * @return
     */
    @GetMapping(value = "getByUserId")
        public Map<String, Object> findByUserId(BaseDepartment baseDepartment) {
        List<BaseDepartment> res=baseDepartmentService.findByUserId(baseDepartment.getId());
        return ResponseInfo.success(res);
    }

    /**
     * 获取表格数据
     * @param zrReport
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "table")
    public Map<String, Object> getAll(ZrReport zrReport, InputParam inputParam) {

        if(!StringUtils.isEmpty(inputParam.getId())){
            String[] departIds = inputParam.getId().split(",");
            zrReport.setDepartIds(departIds);
        }
        if(!StringUtils.isEmpty(zrReport.getTypes())){
            String[] typeIds = zrReport.getTypes().split(",");
            zrReport.setTypeIds(typeIds);
        }
        if(!StringUtils.isEmpty(inputParam.getIncludeIds())){
            String[] ids = inputParam.getIncludeIds().split(",");
            for (String id :
                    ids) {
                inputParam.getIdsList().add(id);
            }
        }
        //如果有错误，就报错
        Map<String, Object> map = zrReportService.getAll(zrReport, inputParam);
        return map;
    }

    /**
     * 编辑返回数据
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "getById")
    public Map<String, Object> getById(InputParam inputParam) {
        Map<String,Object> map=new HashMap();
        if(StringUtils.isEmpty(inputParam.getId())){
            map.put("code",0);
            map.put("msg","参数错误");
            return map;
        }
        //如果有错误，就报错
        ZrReport report = zrReportService.getById(inputParam.getId());
        Map<String, Object> success = ResponseInfo.success(report);
        return success;
    }
    /**
     * 编辑返回数据
     * @param inputParam
     * @return
     */
    @ApiOperation(value = "从个人计划中得到,最近的一次报告",
            notes = "从个人计划中得到,最近的一次报告",
            httpMethod = "POST"
    )
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "id", value = "点击的用户id",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "type", value = "0日，1周，2月，3季，4年",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "startDate", value = "日报的日期",required =
                    false,paramType = "query")
    })
    @RequestMapping(value = "getFormPlan")
    public ResponseBean<ZrReport> getFormPlan(@ApiIgnore InputParam inputParam) {
        ResponseBean<ZrReport> res=zrReportService.getFormPlan(inputParam);
        return res;
    }
    /**
     * 保存修改
     * @param zrReport
     * @return
     */
    @RequestMapping(value = "save")
    public Map<String, Object> save(ZrReport zrReport) {
        if(StringUtils.isEmpty(zrReport.getId())){
            zrReport.setUserId(SysUtil.getLoginUserId(request));
            zrReport.setCreateTime(LocalDateTime.now());
            zrReportService.saveOrUpdate(zrReport);
        }else{
            zrReportService.saveOrUpdate(zrReport);
        }
        return ResponseInfo.success();
    }

    
    /**
     * 删除
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "del")
    public Map<String, Object> del(InputParam inputParam) {
        if(StringUtils.isEmpty(inputParam.getId())){
            return ResponseInfo.error("参数错误");
        }
        String[] ids = inputParam.getId().split(",");
        zrReportService.removeByIds(Arrays.asList(ids));
        return ResponseInfo.success();
    }

    /**
     * 得到统计数目
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "getCount")
    public Map<String, Object> getCount(ZrReport zrReport, InputParam inputParam) {
        if(!StringUtils.isEmpty(inputParam.getId())){
            String[] departIds = inputParam.getId().split(",");
            zrReport.setDepartIds(departIds);
        }
        Map<String,Object> map=zrReportService.getCount(zrReport,inputParam);
        return map;
    }
}

